package com.briup.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @author liaozd
 */
public class URLCodeUtil {

	private final static Logger logger = LoggerFactory.getLogger(URLCodeUtil.class);

	public static void main(String[] args) {
		String urlEncode = encodeUrlParams("BJ8iAEcGRhmxuYSdUZoctrXfGxzieqrlj0rmc3g5KaF4dZm/k8vhftXkuHbKOAlIEobetNMrIT1itSyoCNAXSSOWgMNE36q3aNmIv1MA3Nxt6MEkFW5DxUBXVy5yXCmlDhfyxuCVnsovfgPW");
		System.out.println(urlEncode);
		String urlDecode = decodeUrlParams(urlEncode);
		System.out.println(urlDecode);
	}

	/**
	 * URL 转码
	 * @param param
	 * @return
	 */
	public static String encodeUrlParams(String param) {
		try {
			String urlEncode = URLEncoder.encode(param, "utf-8");
			return urlEncode;
		} catch (UnsupportedEncodingException e) {
			logger.error("validateSignature param={}, 验签异常={}, ", param, e.getMessage());
		}
		return null;
	}

	/**
	 * URL 解码
	 * @param param
	 */
	public static String decodeUrlParams(String param) {
		try {
			String urlDecode = URLDecoder.decode(param, StandardCharsets.UTF_8.toString());
			return urlDecode;
		} catch (UnsupportedEncodingException e) {
			logger.error("validateSignature param={}, 验签异常={}, ", param, e.getMessage());
		}
		return null;
	}

}
